package com.vdg.lockphotos.fragment;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.vdg.lockphotos.DisplayImageActivity;
import com.vdg.lockphotos.ImagesAlbumActivity;
import com.vdg.lockphotos.ListPhotosActivity;
import com.vdg.lockphotos.R;
import com.vdg.lockphotos.app.ApplicationManagement;
import com.vdg.lockphotos.data.Photo;
import com.vdg.lockphotos.gridphotos.GridHiddenPhotoAdapter;
import com.vdg.lockphotos.notify.OnBackPressListener;
import com.vdg.lockphotos.notify.OnLoadListHiddenImageListener;
import com.vdg.lockphotos.util.Utilities;
import com.vdg.lockphotos.view.TextViewPlus;

public class ListHiddenPhotosFragment extends Fragment implements
		OnBackPressListener {
	private GridView mGivPhotos;
	private ArrayList<Photo> mListPhotos = new ArrayList<Photo>();
	private GridHiddenPhotoAdapter mGridPhotoAdapter;
	private Handler mHandler = new Handler();
	private RelativeLayout mLnHide;
	private FrameLayout mFrmDialog;
	private Photo mSelectedPhoto;
	private TextViewPlus mTxvAction;
	private LinearLayout mLnAds;
	private OnLoadListHiddenImageListener mListener = new OnLoadListHiddenImageListener() {

		@Override
		public void onLoadSucessed(ArrayList<Photo> listPhoto) {
			// TODO Auto-generated method stub
			mListPhotos.clear();
			mListPhotos.addAll(listPhoto);
			mGridPhotoAdapter.notifyDataSetChanged();
			mFrmDialog.setVisibility(View.GONE);
		}

		@Override
		public void onLoadFailed() {
			// TODO Auto-generated method stub
			mFrmDialog.setVisibility(View.GONE);
		}

		@Override
		public void onStartLoad() {
			// TODO Auto-generated method stub
			mFrmDialog.setVisibility(View.VISIBLE);
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.grid_hidden_photos_layout,
				container, false);
		init(rootView);
		((ImagesAlbumActivity) getActivity()).regisBackPressListener(this);
		// loadImage();
		// TODO Auto-generated method stub
		return rootView;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		ApplicationManagement.getInstance(getActivity()).loadHideImages(
				mListener);
		super.onResume();
	}

	private void updateInGridView(final String url) {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				int start = mGivPhotos.getFirstVisiblePosition();
				for (int i = start, j = mGivPhotos.getLastVisiblePosition(); i <= j; i++) {
					String urlInList = ((Photo) (mGivPhotos
							.getItemAtPosition(i))).getHiddenUrl();
					if (urlInList != null && urlInList.equals(url)) {
						View view = mGivPhotos.getChildAt(i - start);
						mGivPhotos.getAdapter().getView(i, view, mGivPhotos);
						break;
					}
				}
			}
		});
	}

	private void init(View rootView) {
		mGivPhotos = (GridView) rootView.findViewById(R.id.grd_photos);
		mGridPhotoAdapter = new GridHiddenPhotoAdapter(getActivity(), 0,
				mListPhotos);
		mLnHide = (RelativeLayout) rootView.findViewById(R.id.btn_hide);
		mTxvAction = (TextViewPlus) rootView.findViewById(R.id.txv_action);
		mLnHide.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if (mTxvAction
						.getText()
						.toString()
						.equals(getResources().getString(
								R.string.select_to_unhide))) {
					mTxvAction.setText(getResources().getString(
							R.string.un_hide));
					String message = getActivity().getResources().getString(
							R.string.please_select_photo);
					Utilities.showLongToast(message, getActivity());
					if (mGridPhotoAdapter != null) {
						mGridPhotoAdapter.setSelectAble(true);
						mGridPhotoAdapter.notifyDataSetChanged();
					}

				} else {
					if (mGridPhotoAdapter != null) {
						mGridPhotoAdapter.setSelectAble(false);
						mGridPhotoAdapter.notifyDataSetChanged();
					}
					if (mGridPhotoAdapter.getSelectedData() == null
							|| mGridPhotoAdapter.getSelectedData().size() == 0) {
						// Utilities.showToast(
						// getResources().getString(
						// R.string.please_select_photo),
						// getActivity());
						mTxvAction.setText(getResources().getString(
								R.string.select_to_unhide));
						return;
					} else {
						if (Utilities.isExternalStorageReadable() == true
								&& Utilities.isExternalStorageWritable() == true) {
							ApplicationManagement
									.getInstance(getActivity())
									.unHidePhotos(
											mGridPhotoAdapter.getSelectedData());
							mFrmDialog.setVisibility(View.VISIBLE);
						} else {
							Utilities.showToast(
									getResources().getString(
											R.string.sdcard_notready),
									getActivity());
						}
					}
					mTxvAction.setText(getResources().getString(
							R.string.select_to_unhide));
				}
			}
		});
		mGivPhotos.setAdapter(mGridPhotoAdapter);
		mGivPhotos.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				mSelectedPhoto = mListPhotos.get(position);
				if (mTxvAction
						.getText()
						.toString()
						.equals(getResources().getString(
								R.string.select_to_unhide))) {
					Intent intent = new Intent(getActivity(),
							DisplayImageActivity.class);
					intent.putExtra(DisplayImageActivity.DISPLAY_IMAGE_URL_KEY,
							mSelectedPhoto.getHiddenUrl());
					startActivity(intent);
				} else {
					mListPhotos.get(position).setIsSelected(
							!mListPhotos.get(position).isIsSelected());
					updateInGridView(mListPhotos.get(position).getHiddenUrl());
				}
			}
		});
		mGivPhotos.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				mSelectedPhoto = (mListPhotos.get(position));
				return false;
			}
		});
		mLnAds = (LinearLayout) rootView.findViewById(R.id.ln_ads);
		AdSize testSize = AdSize.createAdSize(AdSize.SMART_BANNER,
				getActivity());
		AdView ad = (AdView) rootView.findViewById(R.id.adView);
		ad.loadAd(new AdRequest().addTestDevice(getResources().getString(
				R.string.test_devices_id)));
		registerForContextMenu(mGivPhotos);
		mFrmDialog = (FrameLayout) rootView.findViewById(R.id.frm_dialog);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		// menu.setHeaderTitle("Menu");
		// menu.add(0, v.getId(), 1,
		// getResources().getString(R.string.view_this_photo));
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		// if (item.getTitle().equals(
		// getResources().getString(R.string.view_this_photo))) {
		// Intent intent = new Intent(getActivity(),
		// DisplayImageActivity.class);
		// // intent.putExtra(DisplayImageActivity.DISPLAY_IMAGE_URL_KEY,
		// // mSelectedPhoto.getHiddenUrl());
		// startActivity(intent);
		// }
		return super.onContextItemSelected(item);
	}

	@Override
	public boolean onBackPress() {
		// TODO Auto-generated method stub
		if (mTxvAction.getText().toString()
				.equals(getResources().getString(R.string.un_hide))) {
			mTxvAction.setText(getResources().getString(
					R.string.select_to_unhide));
			if (mListPhotos != null && mGridPhotoAdapter != null) {
				for (Photo photo : mListPhotos) {
					photo.setIsSelected(false);
				}
				mGridPhotoAdapter.setSelectAble(false);
				mGridPhotoAdapter.notifyDataSetChanged();
			}
			return true;
		}
		return false;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}
